package com.example.administrator.load_broadcast;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private BroadcastReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver=new LoadReseiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("Load_Broadcast");
        LocalBroadcastManager manager=LocalBroadcastManager.getInstance(MainActivity.this);
        manager.registerReceiver(receiver,filter);

        findViewById(R.id.btn_load).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.setAction("Load_Broadcast");
                LocalBroadcastManager manager=LocalBroadcastManager.getInstance(MainActivity.this);
                manager.sendBroadcast(intent);/*本地广播只能通过动态方式注册，因为本地广播就是在应用内部传递的，
                发送广播时，应用肯定已经启动了，所以不需要采用静态注册的方式。*/
            }
        });
    }
}
